﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraLayout;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.UI.Settings
{
    internal static class ControlSetting
    {
        public static void SetProperties(Control control, ref string message)
        {
            if (control is LayoutControl)
            {
                var c = (control as LayoutControl);
                LayoutControlSetting.SetProperties(c);
                message += control.GetType().Name + ":" + control.Name + "," + Environment.NewLine;
            }
            else if (control is GridControl)
            {
                var c = control as GridControl;
                GridSetting.SetGridProperties(c);
                message += control.GetType().Name + ":" + control.Name + "," + Environment.NewLine;
            }
            if (control is BaseEdit)
            {
                var c = control as BaseEdit;
                BaseEditSetting.SetProperties(c);
                message += control.GetType().Name + ":" + control.Name + "," + Environment.NewLine;
            }
            if (control is LookUpEditBase)
            {
                var c = control as LookUpEditBase;
                LookUpEditBaseSetting.SetProperties(c);
            }
            else if (control is DateEdit)
            {
                var c = control as DateEdit;
                DateEditSetting.SetProperties(c);
            }
        }

        public static void SetProperties(Control.ControlCollection controls, ref string message)
        {
            foreach (Control control in controls)
            {
                SetProperties(control, ref message);
                if (control.HasChildren)
                {
                    SetProperties(control.Controls, ref message);
                }
            }
        }

        public static void SetTabIndex(Control.ControlCollection controls, ref string message)
        {
            var yGroup = controls.Cast<Control>().Where(p => !string.IsNullOrEmpty(p.Name)).GroupBy(p => p.Location.Y);
            var yOrderGroup = yGroup.OrderBy(p => p.Key);
            List<Control> list = new List<Control>();
            foreach (var temp in yOrderGroup)
            {
                if (temp.Count() == 1)
                {
                    var c = temp.First();
                    c.TabIndex = list.Count;
                    list.Add(c);
                    message += c.Name + ":" + c.TabIndex + Environment.NewLine;
                    if (c.Controls.Count != 0)
                    {
                        SetTabIndex(c.Controls, ref message);
                    }
                }
                else
                {
                    var order = temp.OrderBy(p => p.Location.X);
                    foreach (var c in order)
                    {
                        c.TabIndex = list.Count;
                        list.Add(c);
                        message += c.Name + ":" + c.TabIndex + Environment.NewLine;
                        if (c.Controls.Count != 0)
                        {
                            SetTabIndex(c.Controls, ref message);
                        }
                    }
                }
            }
        }

    }
}
